import BaseEntity from '@/framework/domain/BaseEntity'
import Id from '@/framework/infra/generator/Id'

const id = Id('spuPropValue')
export default class SpuPropertyValue extends BaseEntity {
  constructor(props, isSelfEdit = false, checked = false, spuProperty = {}) {
    super(props)
    this.checked = checked
    this.notes = ''
    if (spuProperty.spuValues) {
      const spuPropValueMap = spuProperty.spuValues.reduce((r, a) => {
        r[a.id] = a
        return r
      }, {})
      if (spuPropValueMap[this.id]) {
        this.checked = true
        this.notes = spuPropValueMap[this.id].notes
      }
    }
    this.isSelfEdit = isSelfEdit
    this.finalValue = this.value
    this.previewImg = this.previewImg || ''
    // 属性值来源 ,0 : 系统，1：自定义
    this.valueSource = this.valueSource || 0
    this.groupId = this.groupId || (this.group ? this.group.id : '0')
    this.id = this.id || ''
    this.propertyId = spuProperty.id
    this.uploadPicFlag = spuProperty.uploadPicFlag
    this._key = id()
  }

  clear() {
    this.checked = false
    this.notes = ''
  }

  resumeValue() {
    this.value = this.finalValue
  }

  storeValue() {
    this.finalValue = this.value
  }

  setValue(value) {
    this.value = value
  }

  setId(id) {
    if (this.id || !id) {
      return
    }
    this.id = id
  }
}
